import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        redirect: '/index'
    },
    {
        path: '/login',
        component: () => import('@/views/login/UserLogin.vue'),
        hidden: true,
        meta: {requiresAuth: false}
    },
    {
        path: '/404',
        component: () => import('@/views/Error404Component.vue'),
        hidden: true,
        meta: {requiresAuth: false}
    },
    {
        path: '/index',
        component: () => import('@/views/blog/BlogIndex.vue'),
        hidden: true,
        meta: {requiresAuth: false},

    },
    {
        path: '/index/blogInfo',
        meta: {requiresAuth: false},
        component: () => import('@/views/blog/BlogInfo.vue'),
    },
    {
        path: '/space',
        component: () => import('@/views/UserSpace.vue'),
        meta: {requiresAuth: true},
        redirect: '/space/spaceHome',
        children: [
            {
                path: '403',
                name: 'Auth',
                meta: {requiresAuth: false, name: '无权限访问'},
                component: () => import('@/views/space/AuthComponent.vue')
            },
            {
                path: 'userManager',
                meta: {requiresAuth: true, name: '用户数据'},
                component: () => import('@/views/user/UserManager.vue')
            },
            {
                path: 'userInfo',
                meta: {requiresAuth: true, name: '个人信息'},
                component: () => import('@/views/user/UserInfo.vue')
            },
            {
                path: 'updatePwd',
                meta: {requiresAuth: true, name: '修改密码'},
                component: () => import('@/views/user/UpdatePwd.vue')
            },
            {
                path: 'spaceHome',
                meta: {requiresAuth: true, name: '系统首页'},
                component: () => import('@/views/space/SpaceHome.vue')
            },
            {
                path: 'articleManager',
                meta: {requiresAuth: true, name: '文章管理'},
                component: () => import('@/views/articles/ArticleManager.vue')
            },
            {
                path: 'article',
                meta: {requiresAuth: true, name: '博客文章'},
                component: () => import('@/views/articles/ArticleShow.vue')
            },
            {
                path: 'likes',
                meta: {requiresAuth: true, name: '点赞详细'},
                component: () => import('@/views/space/LikesManager.vue')
            },
            {
                path: 'comments',
                meta: {requiresAuth: true, name: '评论详细'},
                component: () => import('@/views/space/CommentsManager.vue')
            },
        ]
    },
    {
        path: '*',
        meta: {name: '未找到文件'},
        redirect: '/404',
        hidden: true
    },
];

const router = new VueRouter({
    routes
})
router.beforeEach((to, from, next) => {
    const userString = sessionStorage.getItem('user-key')
    const user = userString ? JSON.parse(userString) : {}

    // 检查目标路由是否需要认证
    if (to.matched.some(record => record.meta.requiresAuth)) {
        // 如果需要认证，检查用户是否登录
        if (!user.token) {
            next({
                path: '/login',
                query: {redirect: to.fullPath}
            })
        } else {
            next()
        }
    } else {
        next()
    }
})
export default router
